package elm.example.controller;

import elm.example.dao.IFoodDao;
import elm.example.po.Food;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/*
    classDescription:食物信息控制类，实现食物信息的相关服务逻辑
*/
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/food")
@Tag(name = "食物信息相关服务")
public class FoodController {

   @Resource
   private IFoodDao foodDao;

   @Operation(summary = "获取所有食物信息")
   @GetMapping(value = "/findAll")
   public List<Food> findAllFood() {
      return StreamSupport.stream(foodDao.findAll().spliterator(),false)
              .collect(Collectors.toList());
   }

   @Operation(summary = "查找商家对应的食物")
   @Parameter(name="id",description = "商家id")
   @GetMapping(value = "/findFood")
   public List<Food> findFood(@RequestParam(value = "id")Integer businessId) {
      return foodDao.findFoodByBusinessId(businessId);
   }

   @Operation(summary = "新增食物")
   @Parameter(name="food",description = "食物信息JSON格式（不需要手动设置id）")
   @PostMapping(value = "/createFood")
   public Food insert(@RequestBody Food food) {
      return foodDao.save(food);
   }

   @Operation(summary = "修改食物信息")
   @Parameter(name="food",description = "食物信息JSON格式")
   @PutMapping(value = "/updateFood")
   public Boolean update(@RequestBody Food food) {
      foodDao.save(food);
      return true;
   }

   @Operation(summary = "根据食物id修改其价格")
   @Parameters({
           @Parameter(name="newPrice" ,description = "新定价"),
           @Parameter(name="id" ,description = "食物id")
   })
   @PutMapping(value = "/updatePrice")
   public void updatePrice(@RequestParam(value = "newPrice") Float newPrice, @RequestParam(value = "id") Integer foodId) {
      foodDao.updateFoodPriceById(newPrice, foodId);
   }

   @Operation(summary = "删除食物")
   @Parameter(name="id",description = "食物id")
   @DeleteMapping(value = "/deleteFood")
   public Boolean delete(@RequestParam(value = "id")Integer foodId) {
      foodDao.deleteById(foodId);
      return true;
   }
}
